package com.hkfn.rfb.rfb.bean;

import com.hkfn.rfb.common.bean.BaseModel;
import com.hkfn.rfb.common.utils.StringUtils;

import lombok.Data;
import lombok.EqualsAndHashCode;

@Data
@EqualsAndHashCode(callSuper = true)
public class RfbParamModel extends BaseModel {
    private String paramName;
    private String paramUnit;
    private String paramMaxValue;
    private String paramMinValue;
    private String type;// 0int 1double
    private int digit = 3;

    private String stopValueStr;
    private String startValueStr;
    private String intervalValueStr;

    private boolean isSelected;

    public RfbParamModel() {
    }

    public RfbParamModel(String paramName, String paramMaxValue, String paramMinValue, String paramUnit) {
        this.paramName = paramName;
        this.paramUnit = paramUnit;
        this.paramMaxValue = paramMaxValue;
        this.paramMinValue = paramMinValue;
        this.type = "1";
    }

    public RfbParamModel(String paramName, String paramMaxValue, String paramMinValue, String paramUnit, String type) {
        this.paramName = paramName;
        this.paramUnit = paramUnit;
        this.paramMaxValue = paramMaxValue;
        this.paramMinValue = paramMinValue;
        this.type = type;
    }

    /**
     * 获取名称（单位）
     * @return
     */
    public String getParamNameAndUnit(){
        if (StringUtils.isEmpty(paramUnit)){
            return paramName;
        }
        return paramName + "(" + paramUnit + ")";
    }

    public String getParamRandomValue(){
        if (paramName.startsWith("放电")){
            return "0";
        }
        if (!StringUtils.isEmpty(stopValueStr)){
            if ("0".equals(type)){
                // int类型
                int maxValue = Integer.parseInt(stopValueStr);
                int minValue = Integer.parseInt(startValueStr);
                return "" + ((int)(Math.random() * (maxValue - minValue + 1)) + minValue);
            }else{
                // double类型
                double maxValue = Double.parseDouble(stopValueStr);
                double minValue = Double.parseDouble(startValueStr);
                return String.format("%." + digit + "f", (Math.random() * (maxValue - minValue) + minValue));
            }
        }else{
            if ("0".equals(type)){
                // int类型
                int maxValue = Integer.parseInt(paramMaxValue);
                int minValue = Integer.parseInt(paramMinValue);
                return "" + ((int)(Math.random() * (maxValue - minValue + 1)) + minValue);
            }else{
                // double类型
                double maxValue = Double.parseDouble(paramMaxValue);
                double minValue = Double.parseDouble(paramMinValue);
                return String.format("%." + digit + "f", (Math.random() * (maxValue - minValue) + minValue));
            }
        }
    }

    public String getParamRandomValue(int position){
        if (paramName.startsWith("放电")){
            return "0";
        }
        if (paramName.equals("充电功率")){
            double value = (78.805 + 0.006 * position) * 812.536 / 1000;
            return String.format("%." + digit + "f", value);
        }
        if (StringUtils.isEmpty(intervalValueStr)){
            return getParamRandomValue();
        }
        if (!StringUtils.isEmpty(startValueStr)){
            if ("0".equals(type)){
                // int类型
                int intervalValue = Integer.parseInt(intervalValueStr);
                int stopValue = Integer.parseInt(null != stopValueStr?stopValueStr:(intervalValue>0?paramMaxValue:paramMinValue));
                int startValue = Integer.parseInt(startValueStr);

                int value;
                if (intervalValue > 0) {
                    value = startValue + position * intervalValue > stopValue ? stopValue : startValue + position * intervalValue;
                }else {
                    value = startValue + position * intervalValue < stopValue ? stopValue : startValue + position * intervalValue;
                }
                return "" + value;
            }else{
                // double类型
                double intervalValue = Double.parseDouble(intervalValueStr);
                double stopValue = Double.parseDouble(null != stopValueStr?stopValueStr:(intervalValue>0?paramMaxValue:paramMinValue));
                double startValue = Double.parseDouble(startValueStr);
                double value;
                if (intervalValue > 0) {
                    value = startValue + position * intervalValue > stopValue ? stopValue : startValue + position * intervalValue;
                }else {
                    value = startValue + position * intervalValue < stopValue ? stopValue : startValue + position * intervalValue;
                }
                return String.format("%." + digit + "f", value);
            }
        }else{
            if ("0".equals(type)){
                // int类型
                int maxValue = Integer.parseInt(null != stopValueStr?stopValueStr:paramMaxValue);
                int minValue = Integer.parseInt(paramMinValue);
                int intervalValue = Integer.parseInt(intervalValueStr);
                int value = minValue + position * intervalValue > maxValue ? maxValue : minValue + position * intervalValue;
                return "" + value;
            }else{
                // double类型
                double maxValue = Double.parseDouble(null != stopValueStr?stopValueStr:paramMaxValue);
                double minValue = Double.parseDouble(paramMinValue);
                double intervalValue = Double.parseDouble(intervalValueStr);
                double value = minValue + position * intervalValue > maxValue ? maxValue : minValue + position * intervalValue;
                return String.format("%." + digit + "f", value);
            }
        }
    }
}
